<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="baidu-site-verification" content="SCzNOI2Wq6" />
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>
        daozhuang
    </title>
    
<link rel="stylesheet" href="/libs/highlight/styles/monokai-sublime.css">

    
<link rel="stylesheet" href="/libs/font-awesome/css/font-awesome.min.css">

    
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.1"></head>

<body id="bodyx">
    <div class="hd posts">
    <a href="/index.html"><i class="fa fa-reply replay-btn" aria-hidden="true"></i></a>
    <div class="post-title">
        <p>
            详细inputfile + springMVC文件、图片上传讲解
        </p>
        <hr>
    </div>
    <div class="post-content">
        <h2 id="文件上传form表单注意事项！！！"><a href="#文件上传form表单注意事项！！！" class="headerlink" title="文件上传form表单注意事项！！！"></a>文件上传form表单注意事项！！！</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">   &#x2F;* form表单提交时注意 enctype&#x3D;&quot;multipart&#x2F;form-data&quot;</span><br><span class="line">        是必须要加的 至于为什么请自行百度 *&#x2F;</span><br><span class="line">   &lt;form action&#x3D;&quot;接口地址&quot; method&#x3D;&quot;post&quot; enctype&#x3D;&quot;multipart&#x2F;form-data&quot;&gt;</span><br><span class="line">   	&lt;!-- 这是单个文件上传的input标签  也可以上传多张图片</span><br><span class="line">   	 上传多张图片的话就要用第二种 controller方法进行接收 --&gt;</span><br><span class="line">       &lt;input type&#x3D;&quot;file&quot; name&#x3D;&quot;file&quot;&gt;</span><br><span class="line">       </span><br><span class="line">   	&lt;!-- 这是多个input标签  name值不同 使用第三种controller方法接收 --&gt;</span><br><span class="line">       &lt;input type&#x3D;&quot;file&quot; name&#x3D;&quot;file1&quot;&gt;</span><br><span class="line">       &lt;input type&#x3D;&quot;file&quot; name&#x3D;&quot;file2&quot;&gt;</span><br><span class="line">       &lt;input type&#x3D;&quot;file&quot; name&#x3D;&quot;file3&quot;&gt;</span><br><span class="line">......</span><br><span class="line">   &lt;&#x2F;form&gt;</span><br></pre></td></tr></table></figure>
<h2 id="一、-单文、图片件上传controller"><a href="#一、-单文、图片件上传controller" class="headerlink" title="一、 单文、图片件上传controller"></a>一、 单文、图片件上传controller</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;  &lt;input type&#x3D;&quot;file&quot; name&#x3D;&quot;file&quot;&gt;</span><br><span class="line">&#x2F;&#x2F;对于单个文件上传使用的 controller</span><br><span class="line">&#x2F;&#x2F;只需要写一个 MultipartFile  即可</span><br><span class="line">@RequestMapping(&quot;&quot;)</span><br><span class="line">public void csdnConttroller(MultipartFile file)&#123;</span><br><span class="line">    &#x2F;&#x2F;拿到 input file 标签的 name</span><br><span class="line">    file.getName();</span><br><span class="line">    try &#123;</span><br><span class="line">        &#x2F;&#x2F;拿到input流 这一步会抛出异常</span><br><span class="line">        InputStream in &#x3D; file.getInputStream();</span><br><span class="line">        &#x2F;&#x2F;拿到input流之后想怎么操作 自己说了算 想怎么上传就怎么上传</span><br><span class="line">    &#125; catch (IOException e) &#123;</span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="二、多文件、图片上传controller"><a href="#二、多文件、图片上传controller" class="headerlink" title="二、多文件、图片上传controller"></a>二、多文件、图片上传controller</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;* 对于多个文件上传使用的 controller</span><br><span class="line">&#x2F;&#x2F;只需要把 MultipartFile 变成数组形式接收即可</span><br><span class="line">&#x2F;&#x2F;这个方法针对 一个input标签选择多个文件</span><br><span class="line">&#x2F;&#x2F; 或者是多个input标签name是相同的值</span><br><span class="line">&#x2F;&#x2F; &lt;input type&#x3D;&quot;file&quot; name&#x3D;&quot;file&quot;&gt;</span><br><span class="line">@RequestMapping(&quot;2&quot;)  *&#x2F;</span><br><span class="line">public void newCsdn(MultipartFile[] files)&#123;</span><br><span class="line">    for (MultipartFile file: files) &#123;</span><br><span class="line">        &#x2F;&#x2F;拿到 input file 标签的 name</span><br><span class="line">        file.getName();</span><br><span class="line">        try &#123;</span><br><span class="line">            &#x2F;&#x2F;拿到input流 这一步会抛出异常</span><br><span class="line">            InputStream in &#x3D; file.getInputStream();</span><br><span class="line">            &#x2F;&#x2F;拿到input流之后想怎么操作 自己说了算 想怎么上传就怎么上传</span><br><span class="line">        &#125; catch (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="三、多文件、图片上传多个input标签且name值不同"><a href="#三、多文件、图片上传多个input标签且name值不同" class="headerlink" title="三、多文件、图片上传多个input标签且name值不同"></a>三、多文件、图片上传多个input标签且name值不同</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;对于多个文件上传使用的 controller</span><br><span class="line">&#x2F;&#x2F;并且是多个input标签 且name值不一样</span><br><span class="line">&#x2F;&#x2F; &lt;input type&#x3D;&quot;file&quot; name&#x3D;&quot;file&quot;&gt;</span><br><span class="line">@RequestMapping(&quot;3&quot;)</span><br><span class="line">public void inputFiles(HttpServletRequest request)&#123;</span><br><span class="line">    MultipartFile file &#x3D; null;</span><br><span class="line">    CommonsMultipartResolver resolver &#x3D; new CommonsMultipartResolver(request.getSession().getServletContext());</span><br><span class="line">    if (resolver.isMultipart(request)) &#123;</span><br><span class="line">        MultipartHttpServletRequest multipartRequest &#x3D; (MultipartHttpServletRequest) request;</span><br><span class="line">        Iterator&lt;String&gt; fileNames &#x3D; multipartRequest.getFileNames();</span><br><span class="line">        while (fileNames.hasNext()) &#123;</span><br><span class="line">            &#x2F;&#x2F;input name字段值</span><br><span class="line">            String imgname &#x3D; fileNames.next();</span><br><span class="line">            file &#x3D; multipartRequest.getFile(imgname);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
        <hr class="fhr">
        <div id="vcomments"></div>
    
</div>
    <div class="footer" id="footer">
    <p>Copyright © 2020 <a class="flink" href="https://hexo.io" target="_blank" rel="noopener">Hexo</a>-<a class="flink" href="https://github.com/sanjinhub/hexo-theme-geek" target="_blank" rel="noopener">Geek</a>.
        <label class="el-switch el-switch-green el-switch-sm" style="vertical-align: sub;">
            <input type="checkbox" name="switch" id="update_style">
            <span class="el-switch-style"></span>
        </label>
<!--         <script type="text/javascript">
        var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
        document.write(unescape("%3Cspan id='cnzz_stat_icon_1278548644'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "v1.cnzz.com/stat.php%3Fid%3D1278548644%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
        </script> -->
    </p>
</div>
<input type="hidden" id="web_style" value="black">
<input type="hidden" id="valine_appid" value="c994h5wXLa8fSMa424BTyVhs-gzGzoHsz">
<input type="hidden" id="valine_appKey" value="4tFbwCi4sYo3yRFgiNFpQd4N">

<script src="/libs/jquery.min.js"></script>


<script src="/libs/highlight/highlight.pack.js"></script>

<script src='//cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>

<script src="/js/js.js"></script>

<style type="text/css">
.v * {
    color: #698fca;
}

.v .vlist .vcard .vhead .vsys {
    color: #3a3e4a;
}

.v .vlist .vcard .vh .vmeta .vat {
    color: #638fd5;
}

.v .vlist .vcard .vhead .vnick {
    color: #6ba1ff;
}

.v a {
    color: #8696b1;
}

.v .vlist .vcard .vhead .vnick:hover {
    color: #669bfc;
}
</style>
</body>

</html>